/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastraCliente.java
 *
 * Created on 29/03/2012, 01:42:26
 */
package tela;

import bd.Consultas;
import java.awt.Color;
import java.awt.Cursor;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import tabela.Cliente;
import tabela.ResponsavelTecnico;

/**
 *
 * @author Edu
 */
public class CadastraBuscaCliente extends javax.swing.JPanel {

    /**
     * Creates new form CadastraCliente
     */
    private int idClienteSelecionado = 999999;
    private int ERRO = 0;
    private int SUCESSO = 1;
    private int INFO = 2;

    public CadastraBuscaCliente() {
        initComponents();
        preencheCombos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldBuscaCliente = new javax.swing.JTextField();
        jButtonBuscarCliente = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListClientes = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        mensagens = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jTextFieldRazaoSocial = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jTextFieldNomeFantasia = new javax.swing.JTextField();
        jTextFieldEndereco = new javax.swing.JTextField();
        jTextFieldEmail = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jRadioButtonPessoaFisica = new javax.swing.JRadioButton();
        jRadioButtonPessoaJuridica = new javax.swing.JRadioButton();
        jLabel18 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jTextFieldNomeCliente = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jFormattedTextFieldCpf = new javax.swing.JFormattedTextField();
        jFormattedTextFieldRG = new javax.swing.JFormattedTextField();
        jFormattedTextFieldFone = new javax.swing.JFormattedTextField();
        jFormattedTextFieldCnpj = new javax.swing.JFormattedTextField();
        jFormattedTextFieldInscricaoEstadual = new javax.swing.JFormattedTextField();
        jLabel25 = new javax.swing.JLabel();
        jTextFieldCidade = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jFormattedTextFieldCelular = new javax.swing.JFormattedTextField();
        jFormattedTextFieldUF = new javax.swing.JFormattedTextField();
        jFormattedTextFieldCEP = new javax.swing.JFormattedTextField();
        jButtonSalvarCliente = new javax.swing.JButton();
        jButtonExcluirCliente = new javax.swing.JButton();
        jButtonNovoCliente = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jListRespTec = new javax.swing.JList();
        jLabel29 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jComboBoxRespTec = new javax.swing.JComboBox();
        jButtonAddRespTec = new javax.swing.JButton();
        jButtonExcluirRT = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jFormattedTextFieldRenasem = new javax.swing.JFormattedTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Cliente");

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/figura/seta.png"))); // NOI18N
        jLabel7.setMinimumSize(new java.awt.Dimension(10, 256));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 2, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 3, 14), java.awt.Color.gray)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Nome/Razão Social:");

        jTextFieldBuscaCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jButtonBuscarCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonBuscarCliente.setText("Buscar");
        jButtonBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarClienteActionPerformed(evt);
            }
        });

        jListClientes.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jListClientes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListClientesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jListClientes);

        mensagens.setBackground(new java.awt.Color(204, 204, 204));
        mensagens.setColumns(20);
        mensagens.setEditable(false);
        mensagens.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        mensagens.setForeground(new java.awt.Color(204, 0, 0));
        mensagens.setRows(5);
        mensagens.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jScrollPane3.setViewportView(mensagens);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane3)
                        .addComponent(jScrollPane2)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jTextFieldBuscaCliente)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonBuscarCliente)))
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldBuscaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 2, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 3, 14), java.awt.Color.gray)); // NOI18N

        jTextFieldRazaoSocial.setEditable(false);
        jTextFieldRazaoSocial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRazaoSocialActionPerformed(evt);
            }
        });

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("Fantasia:");

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel20.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel20.setText("CNPJ:");

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Razão Social:");

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel15.setText("E-Mail:");

        jTextFieldNomeFantasia.setEditable(false);

        jTextFieldEndereco.setEditable(false);

        jTextFieldEmail.setEditable(false);

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Endereço:");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Fone/Fax:");

        buttonGroup1.add(jRadioButtonPessoaFisica);
        jRadioButtonPessoaFisica.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonPessoaFisica.setText("Física");
        jRadioButtonPessoaFisica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonPessoaFisicaActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonPessoaJuridica);
        jRadioButtonPessoaJuridica.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonPessoaJuridica.setText("Jurídica");
        jRadioButtonPessoaJuridica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonPessoaJuridicaActionPerformed(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel18.setText("Pessoa:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Inscrição Estadual:");

        jLabel24.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel24.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel24.setText("Nome:");

        jTextFieldNomeCliente.setEditable(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("CPF:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("R.G:");

        jFormattedTextFieldCpf.setEditable(false);
        try {
            jFormattedTextFieldCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextFieldCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextFieldCpfActionPerformed(evt);
            }
        });

        jFormattedTextFieldRG.setEditable(false);
        try {
            jFormattedTextFieldRG.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###-#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jFormattedTextFieldFone.setEditable(false);
        try {
            jFormattedTextFieldFone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jFormattedTextFieldCnpj.setEditable(false);
        try {
            jFormattedTextFieldCnpj.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jFormattedTextFieldInscricaoEstadual.setEditable(false);
        try {
            jFormattedTextFieldInscricaoEstadual.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###.###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel25.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel25.setText("Cidade:");

        jTextFieldCidade.setEditable(false);

        jLabel26.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel26.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel26.setText("UF:");

        jLabel27.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel27.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel27.setText("CEP:");

        jLabel28.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel28.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel28.setText("Celular:");

        jFormattedTextFieldCelular.setEditable(false);
        try {
            jFormattedTextFieldCelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jFormattedTextFieldUF.setEditable(false);
        try {
            jFormattedTextFieldUF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("UU")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jFormattedTextFieldCEP.setEditable(false);
        try {
            jFormattedTextFieldCEP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jButtonSalvarCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonSalvarCliente.setText("Salvar");
        jButtonSalvarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarClienteActionPerformed(evt);
            }
        });

        jButtonExcluirCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonExcluirCliente.setText("Excluir");
        jButtonExcluirCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExcluirClienteActionPerformed(evt);
            }
        });

        jButtonNovoCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButtonNovoCliente.setText("Novo Cliente");
        jButtonNovoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovoClienteActionPerformed(evt);
            }
        });

        jListRespTec.setEnabled(false);
        jScrollPane4.setViewportView(jListRespTec);

        jLabel29.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel29.setText("Responsáveis Técnicos:");

        jLabel30.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel30.setText("Adicionar novo Responsável Técnico");

        jComboBoxRespTec.setEnabled(false);

        jButtonAddRespTec.setText("Adicionar");
        jButtonAddRespTec.setEnabled(false);
        jButtonAddRespTec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddRespTecActionPerformed(evt);
            }
        });

        jButtonExcluirRT.setFont(new java.awt.Font("Tahoma", 2, 10)); // NOI18N
        jButtonExcluirRT.setText("Excluir");
        jButtonExcluirRT.setEnabled(false);
        jButtonExcluirRT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExcluirRTActionPerformed(evt);
            }
        });

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel19.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel19.setText("RENASEM:");

        jFormattedTextFieldRenasem.setEditable(false);
        try {
            jFormattedTextFieldRenasem.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("UU-#####/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jFormattedTextFieldFone, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel28, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jFormattedTextFieldCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel25, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel29, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jTextFieldCidade)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel26, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFormattedTextFieldUF, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel27, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFormattedTextFieldCEP, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(jButtonExcluirRT)))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jButtonAddRespTec, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel30, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jComboBoxRespTec, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jButtonNovoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonExcluirCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSalvarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel24, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextFieldEndereco)
                                    .addComponent(jTextFieldNomeCliente)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jFormattedTextFieldCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFormattedTextFieldRG, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jFormattedTextFieldCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFormattedTextFieldInscricaoEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jTextFieldRazaoSocial)
                                    .addComponent(jTextFieldNomeFantasia)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jRadioButtonPessoaFisica, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRadioButtonPessoaJuridica)
                                .addGap(28, 28, 28)
                                .addComponent(jLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jFormattedTextFieldRenasem, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jFormattedTextFieldRenasem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel19))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jRadioButtonPessoaFisica)
                        .addComponent(jRadioButtonPessoaJuridica)
                        .addComponent(jLabel18)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel24)
                    .addComponent(jTextFieldNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jFormattedTextFieldCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jLabel2)
                    .addComponent(jFormattedTextFieldCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldInscricaoEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jTextFieldRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jTextFieldNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextFieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(jTextFieldCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel26)
                    .addComponent(jLabel27)
                    .addComponent(jFormattedTextFieldUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jFormattedTextFieldFone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel28)
                    .addComponent(jFormattedTextFieldCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel29)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExcluirRT))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel30)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxRespTec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAddRespTec)))
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvarCliente)
                    .addComponent(jButtonExcluirCliente)
                    .addComponent(jButtonNovoCliente))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(75, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void preencheListaBusca(List resultList) {
        Vector tableData = new Vector();
        for (Object o : resultList) {
            Cliente cliente = (Cliente) o;
            String nome = cliente.getNomeCliente();
            if (nome == null) { //pessoa jurica
                nome = cliente.getRazaoSocialCliente();
            }
            tableData.add(nome);
            System.out.println(nome);
        }
        if (tableData.size() == 0) {
            tableData.add("Nenhuma ocorrência");
        }
        jListClientes.setListData(tableData);
    }

    private void jTextFieldRazaoSocialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRazaoSocialActionPerformed
    }//GEN-LAST:event_jTextFieldRazaoSocialActionPerformed

    private void jRadioButtonPessoaFisicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonPessoaFisicaActionPerformed
        jFormattedTextFieldCpf.setEditable(true);
        jFormattedTextFieldCnpj.setEditable(false);
        jFormattedTextFieldCnpj.setText("");
        jTextFieldEndereco.setEditable(true);
        jTextFieldEmail.setEditable(true);
        jFormattedTextFieldFone.setEditable(true);
        jTextFieldNomeCliente.setEditable(true);
        jFormattedTextFieldRG.setEditable(true);
        jFormattedTextFieldInscricaoEstadual.setEditable(false);
        jFormattedTextFieldInscricaoEstadual.setText("");
        jTextFieldNomeFantasia.setEditable(false);
        jTextFieldNomeFantasia.setText("");
        jTextFieldRazaoSocial.setEditable(false);
        jTextFieldRazaoSocial.setText("");
        jTextFieldCidade.setEditable(true);
        jFormattedTextFieldUF.setEditable(true);
        jFormattedTextFieldCEP.setEditable(true);
        jFormattedTextFieldCelular.setEditable(true);
        jButtonAddRespTec.setEnabled(true);
        jButtonExcluirRT.setEnabled(true);
        jComboBoxRespTec.setEnabled(true);
        jFormattedTextFieldRenasem.setEnabled(true);
        jFormattedTextFieldRenasem.setEditable(true);
        jListRespTec.setEnabled(true);

    }//GEN-LAST:event_jRadioButtonPessoaFisicaActionPerformed

    private void jRadioButtonPessoaJuridicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonPessoaJuridicaActionPerformed
        jFormattedTextFieldCpf.setEditable(false);
        jFormattedTextFieldCpf.setText("");
        jFormattedTextFieldCnpj.setEditable(true);
        jTextFieldEndereco.setEditable(true);
        jTextFieldEmail.setEditable(true);
        jFormattedTextFieldFone.setEditable(true);
        jTextFieldNomeCliente.setEditable(false);
        jTextFieldNomeCliente.setText("");
        jFormattedTextFieldRG.setEditable(false);
        jFormattedTextFieldRG.setText("");
        jFormattedTextFieldInscricaoEstadual.setEditable(true);
        jTextFieldNomeFantasia.setEditable(true);
        jTextFieldRazaoSocial.setEditable(true);
        jTextFieldCidade.setEditable(true);
        jFormattedTextFieldUF.setEditable(true);
        jFormattedTextFieldCEP.setEditable(true);
        jFormattedTextFieldCelular.setEditable(false);
        jFormattedTextFieldCelular.setText("");
        jButtonAddRespTec.setEnabled(true);
        jButtonExcluirRT.setEnabled(true);
        jComboBoxRespTec.setEnabled(true);
        jFormattedTextFieldRenasem.setEnabled(true);
        jFormattedTextFieldRenasem.setEditable(true);
        jListRespTec.setEnabled(true);
    }//GEN-LAST:event_jRadioButtonPessoaJuridicaActionPerformed

    private void jButtonBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarClienteActionPerformed
        alteraCursor(1);
        String sql = "from Cliente where upper(nomeCliente) like upper('%" + jTextFieldBuscaCliente.getText() + "%') OR upper(razaoSocialCliente) like upper('%" + jTextFieldBuscaCliente.getText() + "%')";
        preencheListaBusca(Consultas.getListObjetos(sql));
        alteraCursor(0);

    }//GEN-LAST:event_jButtonBuscarClienteActionPerformed

    private void jListClientesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListClientesValueChanged
        if (jListClientes.getSelectedValue() == null) {
            return;
        }
        limpaComponentes();
        System.out.println("Selecionado=" + jListClientes.getSelectedValue());
        String sql = "from Cliente where nomeCliente='" + jListClientes.getSelectedValue().toString() + "' OR razaoSocialCliente='" + jListClientes.getSelectedValue().toString() + "'";
        List resultList = Consultas.getListObjetos(sql);
        Cliente cliente;
        for (Object o : resultList) {
            cliente = (Cliente) o;
            idClienteSelecionado = cliente.getIdCliente();
            if (cliente.getPessoaCliente() == 0) { //pessoa fisica
                jRadioButtonPessoaFisica.setSelected(true);
                jRadioButtonPessoaJuridica.setSelected(false);
                jRadioButtonPessoaFisicaActionPerformed(null);
                jTextFieldNomeCliente.setText(cliente.getNomeCliente());
                jFormattedTextFieldCpf.setText(cliente.getCpfCliente());
                jFormattedTextFieldRG.setText(cliente.getRgCliente());
                jFormattedTextFieldCelular.setText(cliente.getFoneCelularCliente());
            } else { //pessoa juridica
                jRadioButtonPessoaJuridica.setSelected(true);
                jRadioButtonPessoaFisica.setSelected(false);
                jRadioButtonPessoaJuridicaActionPerformed(null);
                jFormattedTextFieldCnpj.setText(cliente.getCnpjCliente());
                jFormattedTextFieldInscricaoEstadual.setText(cliente.getInscricaoEstadualCliente());
                jTextFieldRazaoSocial.setText(cliente.getRazaoSocialCliente());
                jTextFieldNomeFantasia.setText(cliente.getFantasiaCliente());
            }
            jTextFieldEndereco.setText(cliente.getEnderecoCliente());
            jTextFieldCidade.setText(cliente.getCidadeCliente());
            jFormattedTextFieldUF.setText(cliente.getUfCliente());
            jFormattedTextFieldCEP.setText(cliente.getCepCliente());
            jFormattedTextFieldFone.setText(cliente.getFoneCliente());
            jTextFieldEmail.setText(cliente.getEmailCliente());
            jFormattedTextFieldRenasem.setText(cliente.getRenasemCliente());


//        sql = "from RespTecCliente where cliente =" + idClienteSelecionado;
//        resultList = Consultas.getListObjetos(sql);

            Vector tableData = new Vector();
            for (Object c : cliente.getResponsavelTecnicos()) {
                // ResptecCliente rtc = (ResptecCliente) c;
//                ResponsavelTecnico rt = rtc.getResponsavelTecnico();
                ResponsavelTecnico rt = (ResponsavelTecnico) c;
                String nome = rt.getNomeRespTec();
                tableData.add(nome);
                System.out.println(nome);
            }
            if (tableData.size() == 0) {
                tableData.add("Nenhuma ocorrência");
            }
            jListRespTec.setListData(tableData);
        }
    }//GEN-LAST:event_jListClientesValueChanged

    public void preencheCombos() {

        String sql = "from ResponsavelTecnico";
        List resultList = Consultas.getListObjetos(sql);

        for (Object c : resultList) {
            ResponsavelTecnico rt = (ResponsavelTecnico) c;
            String nome = rt.getNomeRespTec();
            jComboBoxRespTec.addItem(nome);
        }
        if (resultList.size() == 0) {
            jComboBoxRespTec.addItem("Não existe Responsável Técnico cadastrado");
        }
    }

    private void jButtonNovoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovoClienteActionPerformed
        limpaComponentes();
        jRadioButtonPessoaFisica.setSelected(true);
        jRadioButtonPessoaFisicaActionPerformed(null);
        idClienteSelecionado = 999999;
        jListClientes.clearSelection();
        mensagem("Preencha os dados do cliente", INFO);
    }//GEN-LAST:event_jButtonNovoClienteActionPerformed

    private int getIdDisponivel() {
        boolean achou = false;
        int quantidade = Consultas.getCount("select count(*) from Cliente");
        int novoID = quantidade + 1;
        while (!achou) {
            int existeEsseId = Consultas.getCount("select count(idCliente) from Cliente where idCliente =" + novoID);
            if (existeEsseId == 0) {
                return novoID;
            }
            novoID += 1;
        }
        return 0;
    }

    public boolean verificaErroCampos(int pessoa) {
        boolean erro = false;
        String mensagem = "";
        String campos = "";
        
        if (pessoa == 0) {
            if (jTextFieldNomeCliente.getText().equals("")) {
                erro = true;
                campos += "\n- Nome";
            }
            if (jFormattedTextFieldCpf.getText().equals("   .   .   -  ")) {
                erro = true;
                campos += "\n- CPF";
            }
            if (jFormattedTextFieldRG.getText().equals("  .   .   - ")) {
                erro = true;
                campos += "\n- RG";
            }
        } else {
            
            if (jFormattedTextFieldCnpj.getText().equals("  .   .   /    -  ")) {
                erro = true;
                campos += "\n- CNPJ";
            }
            if (jTextFieldRazaoSocial.getText().equals("")) {
                erro = true;
                campos += "\n- Razão Social";
            }
        }
        if (jFormattedTextFieldRenasem.getText().equals("  -     /    ")) {
                erro = true;
                campos += "\n- Razão Social";
            }
        if (erro) {
            mensagem = "Preencha corretamente os seguintes campos:" + campos;
            mensagem(mensagem, ERRO);
        }
        return erro;
    }

    private void mensagem(String msg, int tipo) {
        if (tipo == 0) { //erro
            mensagens.setForeground(Color.RED);
            mensagens.setText(msg);
        } else if (tipo == 1) { //sucesso
            mensagens.setForeground(Color.BLUE);
            mensagens.setText(msg);
        }
        if (tipo == 2) { //info
            mensagens.setForeground(Color.black);
            mensagens.setText(msg);
        }
    }

    private void jButtonSalvarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarClienteActionPerformed

        int pessoa = 1;
        if (jRadioButtonPessoaFisica.isSelected()) {
            pessoa = 0;
        }
        if (verificaErroCampos(pessoa)) {
            return;
        }
        alteraCursor(1);
        int idCliente = idClienteSelecionado;
        if (idClienteSelecionado == 999999) {//novo cliente
            idCliente = getIdDisponivel();
        }
        Cliente cliente = new Cliente(idCliente);
        if (pessoa == 0) { //pessoa fisica   
            cliente.setPessoaCliente(pessoa);
            cliente.setNomeCliente(jTextFieldNomeCliente.getText());
            cliente.setCpfCliente(jFormattedTextFieldCpf.getText());
            cliente.setRgCliente(jFormattedTextFieldRG.getText());
            cliente.setFoneCelularCliente(jFormattedTextFieldCelular.getText());
            cliente.setCnpjCliente(null);
            cliente.setInscricaoEstadualCliente(null);
            cliente.setRazaoSocialCliente(null);
            cliente.setFantasiaCliente(null);
        } else { //pessoa juridica
            cliente.setPessoaCliente(pessoa);
            cliente.setCnpjCliente(jFormattedTextFieldCnpj.getText());
            cliente.setInscricaoEstadualCliente(jFormattedTextFieldInscricaoEstadual.getText());
            cliente.setRazaoSocialCliente(jTextFieldRazaoSocial.getText());
            cliente.setFantasiaCliente(jTextFieldNomeFantasia.getText());
            cliente.setNomeCliente(null);
            cliente.setCpfCliente(null);
            cliente.setRgCliente(null);
            cliente.setFoneCelularCliente(null);
        }
        cliente.setEnderecoCliente(jTextFieldEndereco.getText());
        cliente.setCidadeCliente(jTextFieldCidade.getText());
        cliente.setUfCliente(jFormattedTextFieldUF.getText());
        cliente.setCepCliente(jFormattedTextFieldCEP.getText());
        cliente.setFoneCliente(jFormattedTextFieldFone.getText());
        cliente.setEmailCliente(jTextFieldEmail.getText());
        cliente.setRenasemCliente(jFormattedTextFieldRenasem.getText());

        //responsaveis tecnicos
        Set rts = cliente.getResponsavelTecnicos();
        int size = jListRespTec.getModel().getSize();
        for (int i = 0; i < size; i++) {
            Object item = jListRespTec.getModel().getElementAt(i);
            if (item.toString().equals("Nenhuma ocorrência")) {
                continue;
            }
            Object o = Consultas.getUmaLinha("from ResponsavelTecnico where nomeRespTec='" + item.toString() + "'");
            ResponsavelTecnico rt = (ResponsavelTecnico) o;
            if (o != null) {
                rts.add(o);
            }
        }
        cliente.setResponsavelTecnicos(rts);

        if (Consultas.salvaNoBanco(cliente)) {
            idClienteSelecionado = idCliente;
            if (cliente.getPessoaCliente() == 0) {
                jListClientes.setSelectedValue(cliente.getNomeCliente(), true);
            } else {
                jListClientes.setSelectedValue(cliente.getRazaoSocialCliente(), true);
            }
            jButtonBuscarClienteActionPerformed(null);
            mensagem("Cliente salvo com sucesso!", ERRO);
            String mensagem = "Cliente salvo com sucesso!";
            mensagem(mensagem, SUCESSO);
            //JOptionPane.showMessageDialog(null, "Cliente salvo com sucesso", "Salvar", JOptionPane.INFORMATION_MESSAGE);
            alteraCursor(0);
        } else {
            alteraCursor(0);
            String mensagem = "Erro ao salvar cliente!";
            mensagem(mensagem, ERRO);
            //JOptionPane.showMessageDialog(null, "Erro ao salvar cliente", "Erro", JOptionPane.ERROR);
        }
    }//GEN-LAST:event_jButtonSalvarClienteActionPerformed

    private void jButtonExcluirClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExcluirClienteActionPerformed
        alteraCursor(1);
        String sql = "from Cliente where nomeCliente='" + jListClientes.getSelectedValue().toString() + "' OR razaoSocialCliente='" + jListClientes.getSelectedValue().toString() + "'";
        List resultList = Consultas.getListObjetos(sql);
        if (resultList.size() == 1) {
            Cliente cliente = (Cliente) resultList.get(0);
            if (Consultas.removeDoBanco(cliente)) {
                limpaComponentes();
                jButtonBuscarClienteActionPerformed(null);
                alteraCursor(0);
                mensagem("Cliente excluído com sucesso!", SUCESSO);
                //JOptionPane.showMessageDialog(null, "Cliente excluído com sucesso", "Salvar", JOptionPane.INFORMATION_MESSAGE);
            } else {
                alteraCursor(0);
                mensagem("Erro ao excluir cliente!", ERRO);
                //  JOptionPane.showMessageDialog(null, "Erro ao excluir cliente", "Erro", JOptionPane.ERROR);
            }
        } else {
            alteraCursor(0);
            mensagem("Erro ao excluir cliente!", ERRO);
            // JOptionPane.showMessageDialog(null, "Erro ao excluir!", "Atenção", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButtonExcluirClienteActionPerformed

    private void jFormattedTextFieldCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextFieldCpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextFieldCpfActionPerformed

    private void jButtonAddRespTecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddRespTecActionPerformed


        Object selecionado = jComboBoxRespTec.getSelectedItem();

        if (selecionado.toString().equals("Não existe Responsável Técnico cadastrado")) {
            return;
        }
        if (selecionado.toString().equals("Nenhuma ocorrência")) {
            return;
        }
        Vector tableData = new Vector();
        int size = jListRespTec.getModel().getSize();
        for (int i = 0; i < size; i++) {
            Object item = jListRespTec.getModel().getElementAt(i);

            if (item.toString().equals("Nenhuma ocorrência")) {
                continue;
            }
            if (item.equals(selecionado)) {
                return;
            } else {
                tableData.add(item);
            }
        }

        tableData.add(selecionado);
        jListRespTec.setListData(tableData);

    }//GEN-LAST:event_jButtonAddRespTecActionPerformed

    private void jButtonExcluirRTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExcluirRTActionPerformed
        Vector tableData = new Vector();
        int size = jListRespTec.getModel().getSize();
        for (int i = 0; i < size; i++) {
            Object item = jListRespTec.getModel().getElementAt(i);
            if (item.toString().equals("Nenhuma ocorrência")) {
                continue;
            }
            if (item.equals(jListRespTec.getSelectedValue())) {
                continue;
            } else {
                tableData.add(item);
            }
        }
        jListRespTec.setListData(tableData);
    }//GEN-LAST:event_jButtonExcluirRTActionPerformed

    public void limpaComponentes() {
        jFormattedTextFieldRG.setText("");
        jFormattedTextFieldCpf.setText("");
        jFormattedTextFieldCnpj.setText("");
        jTextFieldEndereco.setText("");
        jTextFieldEmail.setText("");
        jFormattedTextFieldFone.setText("");
        jTextFieldNomeCliente.setText("");
        jFormattedTextFieldRG.setText("");
        jFormattedTextFieldInscricaoEstadual.setText("");
        jTextFieldNomeFantasia.setText("");
        jTextFieldRazaoSocial.setText("");
        jTextFieldCidade.setText("");
        jFormattedTextFieldUF.setText("");
        jFormattedTextFieldCEP.setText("");
        jFormattedTextFieldCelular.setText("");
        jFormattedTextFieldRenasem.setText("");
        jListRespTec.setListData(new Vector());
    }

    public void alteraCursor(int tipo) {
        if (tipo == 1) {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        }
        if (tipo == 0) {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonAddRespTec;
    private javax.swing.JButton jButtonBuscarCliente;
    private javax.swing.JButton jButtonExcluirCliente;
    private javax.swing.JButton jButtonExcluirRT;
    private javax.swing.JButton jButtonNovoCliente;
    private javax.swing.JButton jButtonSalvarCliente;
    private javax.swing.JComboBox jComboBoxRespTec;
    private javax.swing.JFormattedTextField jFormattedTextFieldCEP;
    private javax.swing.JFormattedTextField jFormattedTextFieldCelular;
    private javax.swing.JFormattedTextField jFormattedTextFieldCnpj;
    private javax.swing.JFormattedTextField jFormattedTextFieldCpf;
    private javax.swing.JFormattedTextField jFormattedTextFieldFone;
    private javax.swing.JFormattedTextField jFormattedTextFieldInscricaoEstadual;
    private javax.swing.JFormattedTextField jFormattedTextFieldRG;
    private javax.swing.JFormattedTextField jFormattedTextFieldRenasem;
    private javax.swing.JFormattedTextField jFormattedTextFieldUF;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jListClientes;
    private javax.swing.JList jListRespTec;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButtonPessoaFisica;
    private javax.swing.JRadioButton jRadioButtonPessoaJuridica;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldBuscaCliente;
    private javax.swing.JTextField jTextFieldCidade;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldEndereco;
    private javax.swing.JTextField jTextFieldNomeCliente;
    private javax.swing.JTextField jTextFieldNomeFantasia;
    private javax.swing.JTextField jTextFieldRazaoSocial;
    private javax.swing.JTextArea mensagens;
    // End of variables declaration//GEN-END:variables
}
